﻿using RuoVea.OmiSystem.Attributes;
using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApi.SystemApp.Entitys;
using RuoVea.OmiApi.SystemApp.Service.Dto;
using RuoVea.OmiApi.SystemApp.Service;

namespace RuoVea.OmiSystem.Controllers;

/// <summary>
/// 职位 
/// </summary> 
[AppRoute]
public class PositionController : Controller
{
    private readonly SysPosService _sysPosService;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysPosService"></param>
    public PositionController(SysPosService sysPosService)
    {
        _sysPosService = sysPosService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(Index))]
    public IActionResult Index() { return View(); }

    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysPosition>>> GetPagesAsync([FromBody] PosInput data)
    {
        RestfulResult<PageResult<SysPosition>> restfulResult = new() { Code = CodeStatus.OK, Data=new PageResult<SysPosition>(1,200)};
        restfulResult.Data.Rows = await _sysPosService.GetList(data);
        restfulResult.Data.TotalRows = restfulResult.Data.Rows?.Count()??0;
        return restfulResult;
    }
    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpGet("Pos")]
    public async Task<RestfulResult<List<SysPosition>>> GetPosAsync([FromQuery] PosInput data)
    {
        RestfulResult<List<SysPosition>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _sysPosService.GetList(data);
        return restfulResult;
    }
    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(AddPosInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysPosService.AddPos(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(UpdatePosInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysPosService.UpdatePos(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(DeletePosInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysPosService.DeletePos(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
}